//
//  LJPictureView.m
//  百思不得姐
//
//  Created by 刘鹿杰 on 16/8/28.
//  Copyright © 2016年 刘鹿杰. All rights reserved.
//

#import "LJPictureView.h"
#import <UIImageView+WebCache.h>
#import "LJTopicsModel.h"
#import "DALabeledCircularProgressView.h"
#import "LJShowBigPictureViewController.h"



@interface LJPictureView ()
/** imagePicture 图片显示 */
@property (weak, nonatomic) IBOutlet UIImageView *imagePictureView;
/** progressView 进度条控件 */
@property (nonatomic, weak) IBOutlet DALabeledCircularProgressView *labelProgressView;
/** gifView  */
@property (weak, nonatomic) IBOutlet UIImageView *gitView;
/** 点击查看全图  */
@property (weak, nonatomic) IBOutlet UIButton *allPictureBtn;


@end


@implementation LJPictureView
// 创建pictureView
+(LJPictureView *)pictureView{
    
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].firstObject;
}

-(void)awakeFromNib{
    self.autoresizingMask = UIViewAutoresizingNone;
    
    self.imagePictureView.userInteractionEnabled = YES;
    
    [self.imagePictureView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iamgePictureViewGestureTap)]];
}


-(void)iamgePictureViewGestureTap{
    
    NSLog(@"123");
    // 弹出控制器
    LJShowBigPictureViewController *showPictureVC = [[LJShowBigPictureViewController alloc] init];
    showPictureVC.topicsModel = self.topicModel;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:showPictureVC animated:NO completion:nil];
}


-(void)setTopicModel:(LJTopicsModel *)topicModel{
    
    _topicModel = topicModel;
    
    // 判断是否隐藏gitview
    NSString *extension = topicModel.big_image0.pathExtension;
    self.gitView.hidden = ![extension.lowercaseString isEqualToString:@"gif"];
    
    // 判断是否显示button
    self.allPictureBtn.hidden = !topicModel.isBigPicture;
    
    [self.labelProgressView setProgress:topicModel.pictureProgress animated:NO];
    
    // 赋值
    [self.imagePictureView sd_setImageWithURL:[NSURL URLWithString:topicModel.big_image0] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        self.labelProgressView.hidden = NO;
        
        topicModel.pictureProgress = 1.0 * receivedSize / expectedSize;

        [self.labelProgressView setProgress:topicModel.pictureProgress animated:NO];
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        self.labelProgressView.hidden = YES;
        
        if (!topicModel.isBigPicture) return;
        // 关闭图形上下文
        UIGraphicsBeginImageContextWithOptions(topicModel.pictureFrame.size, YES, 0.0);
        
        CGFloat width = topicModel.pictureFrame.size.width;
        CGFloat height = image.size.height * width / image.size.width;
        
        [image drawInRect:CGRectMake(0, 0, width, height)];
 
        self.imagePictureView.image = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
    
    }];
    
    
}


@end
